package com.example.tool.controller;

import com.example.tool.R.R;
import com.example.tool.entity.Dict;
import com.example.tool.exception.dict.DictIsNotExistException;
import com.example.tool.exception.dict.DictTypeIsExistException;
import com.example.tool.service.DictService;
import com.example.tool.vo.DictReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @GetMapping("/page")
    public R page(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "rows",required = false,defaultValue = "5") Integer rows,
            @RequestBody DictReqVO dictReqVO
            ){
        return R.success(dictService.selectPage(page, rows,dictReqVO));
    }

    @PostMapping("/add")
    public R add(@Validated @RequestBody Dict dict) throws DictTypeIsExistException {
        dictService.add(dict);
        return R.success("添加成功!");
    }

    @PutMapping("/modify")
    public R modify(@Validated @RequestBody Dict dict) throws DictTypeIsExistException {
        dictService.modify(dict);
        return R.success("修改成功!");
    }

    @DeleteMapping("/del/{id}")
    public R add(@PathVariable(name = "id") String id) throws DictIsNotExistException {
        dictService.delById(id);
        return R.success("删除成功!");
    }


}
